define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'learn/normal/index' + location.search,
                    add_url: 'learn/normal/add',
                    edit_url: 'learn/normal/edit',
                    del_url: 'learn/normal/del',
                    multi_url: 'learn/normal/multi',
                    table: 'learn_cate_course1',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'learncourse.name', title: __('Learncourse.name'),operate:'LIKE'},
                        {field: 'learn_cate_id', title: __('Learn_cate_id'), searchList: {"1":__('Learn_cate_id 1'),"2":__('Learn_cate_id 2'),"3":__('Learn_cate_id 3')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'learncourse.origin_price', title: __('Learncourse.origin_price'), operate:'BETWEEN'},
                        {field: 'learncourse.price', title: __('Learncourse.price'), operate:'BETWEEN'},
                        {field: 'learncourse.visitor_origin_price', title: __('Learncourse.visitor_origin_price'), operate:false},
                        {field: 'learncourse.visitor_price', title: __('Learncourse.visitor_price'), operate:false},
                        {field: 'learncourse.buy_numbers', title: __('Learncourse.buy_numbers'), operate:'>='},
                        {field: 'learncourse.image', title: __('Learncourse.image'), operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                search:true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                    $(".columns-right").hide();
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Table.api.init({
                extend: {
                    add_url: 'learn/normal/add',
                    table: 'course1_addcourse'
                }
            });
            var table1 = $('#table1');

            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.add_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Course.Id'),operate:false},
                        {field: 'name', title: __('Course.name'), operate:'LIKE'},
                        {field: 'origin_price', title: __('Course.origin_price'), operate:'BETWEEN'},
                        {field: 'price', title: __('Course.price'), operate:'BETWEEN'},
                        {field: 'visitor_origin_price', title: __('Course.Visitor_origin_price') ,operate:false},
                        {field: 'visitor_price', title: __('Course.Visitor_price'),operate:false},
                        {field: 'buy_numbers', title: __('Course.Buy_numbers'), operate:'>='},
                        {field: 'image', title: __('Course.image'),operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'thumbs', title: __('Course.Thumbs'),operate:false},
                        {field: 'is_buy', title: __('Course.Is_buy'),operate:false, searchList: {"0":__('Course.Is_buy 0'),"1":__('Course.Is_buy 1')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'),operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Course.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:true
            });
            // 为表格绑定事件
            Table.api.bindevent(table1);
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(table1);  //所有选中的项
                var page = table1.bootstrapTable('getData');  //当前页面
                var url = 'special/bindcourse';
                Layer.confirm("<form id='check-piliang' class='form-horizontal'  action='" + Fast.api.fixurl(url) + "' method='post'>" +
                    "<input type='hidden' name='ids' value='' />" +
                    "<div class='form-group'>" +
                    "        <label class='control-label col-xs-12 col-sm-5'>分类类别:</label>" +
                    "        <div class='col-xs-12 col-sm-5' style='margin-top: 7px;'>" +
                    "           <select name='learn_cate_id' id='learn_cate_id'><option value='1'>家长课程</option><option value='2'>教职课程</option><option value='3'>学生课程</option></select>"+
                    "        </div>" +
                    "    </div>"+
                    "<div class='form-group'>" +
                    "        <label class='control-label col-xs-12 col-sm-5'>课程类别:</label>" +
                    "        <div class='col-xs-12 col-sm-5' style='margin-top: 7px;'>" +
                    "           <select name='type' id='learn_cate_type'><option value='1'>幼儿</option><option value='2'>小学</option><option value='3'>初中</option><option value='4'>高中</option></select>"+
                    "    </div>"+
                    "</div>"+
                    "</form>",
                    {
                        title: '批量添加普通分类课程',
                        btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)"],
                        yes: function (index, layero) {
                            submitExportForm(ids.join(","), layero, index);
                            return false;
                        },
                        btn2: function (index, layero) {
                            var ids = [];
                            $.each(page, function (i, j) {
                                ids.push(j.id);
                            });
                            submitExportForm(ids.join(","), layero, index);

                            return false;
                        },
                    });
            });
            var submitExportForm = function (ids, layero, index) {
                var data = {
                    ids: ids,
                    learn_cate_id: $("#learn_cate_id", layero).val(),
                    type: $("#learn_cate_type", layero).val()
                };
                Fast.api.ajax({
                    url: Fast.api.fixurl('learn/normal/bindcourse'),
                    data: data
                }, function (res) {
                    Layer.closeAll();
                    Fast.api.close(layero.selector);
                    top.window.$("ul.nav-addtabs .active").trigger("click");
                });
            };
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});